// 这个文件，专门处理用户相关的
const express = require('express')
const bcryptjs = require('bcryptjs')
const router = express.Router()
var ObjectId = require('mongodb').ObjectId
const UserModel = require('../models/user')

// 添加用户 传参:username,password,phone
router.post('/addUserInfo', async (req, res) => {
  const username = req.body.username
  const password = req.body.password
  const phone = req.body.phone
  const balance = req.body.balance
  const address = req.body.address
  const user = await UserModel.findOne({ phone })

  const nowdate = parseInt(Date.now() / 1000)
  const enddate = nowdate + 7 * 24 * 60 * 60
  let obj = {
    available: 1,
    condition: '无使用门槛\n最多优惠10元',
    reason: '已过期',
    value: 1000,
    name: '新人白给券',
    startAt: nowdate,
    endAt: enddate,
    valueDesc: '10',
    unitDesc: '元'
  }
  // console.log(username, password, phone)

  if (user) {
    // 判断是否有相同手机号
    res.send({
      code: -1,
      msg: '该手机号已被注册'
    })
  } else {
    const user = new UserModel({
      username: username,
      phone: phone,
      address,
      balance,
      password: await bcryptjs.hash(password, 10),
      coupon: JSON.stringify([obj])
    })

    await user.save()

    res.send({
      code: 0,
      msg: '注册成功'
    })
  }
})

// 删除用户 传参:userId
router.post('/delUserInfo', async (req, res) => {
  const userId = req.body.key
  console.log(userId)

  await UserModel.deleteOne({ _id: ObjectId(userId) }, { justOne: true })

  res.send({
    code: 0,
    msg: '删除成功'
  })
})

// 查询所有用户信息 不需要传参
router.post('/findAllUserInfo', async (req, res) => {
  const UserList = await UserModel.find()
  let data = UserList.map(value => {
    let userInfo = {
      userId: value._id,
      username: value.username,
      phone: value.phone,
      address: value.address,
      balance: value.balance,
      coupon: value.coupon
    }
    return userInfo
  })
  // console.log(data)

  res.send({
    code: 0,
    msg: '查询成功',
    data
  })
})

// 根据用户名查询 传参:username
router.post('/findUserInfoByName', async (req, res) => {
  const username = req.body.username
  const user = await UserModel.find({ username })
  let data = user.map(value => {
    let userInfo = {
      userId: value._id,
      username: value.username,
      phone: value.phone,
      address: value.address,
      balance: value.balance,
      coupon: value.coupon
    }
    return userInfo
  })

  res.send({
    code: 0,
    msg: '查询成功',
    data
  })
})

// 通过手机号查询 传参:phone
router.post('/findOneUserInfoByPhone', async (req, res) => {
  const phone = req.body.phone
  const user = await UserModel.find({ phone })

  let data = {
    userId: user[0]._id,
    username: user[0].username,
    phone: user[0].phone,
    address: user[0].address,
    balance: user[0].balance,
    coupon: user[0].coupon
  }

  res.send({
    code: 0,
    msg: '查询成功',
    data
  })
})

// 修改用户信息 传参:userId
router.post('/upDateUserInfo', async (req, res) => {
  const userId = req.body.userId

  const user = await UserModel.find({ _id: ObjectId(userId) })
  const username = req.body.username || user[0].username
  let password = req.body.password || user[0].password
  if (req.body.password) {
    password = await bcryptjs.hash(password, 10)
  }
  const phone = req.body.phone || user[0].phone
  const address = req.body.address || user[0].address
  const coupon = req.body.coupon || user[0].coupon
  const balance = req.body.balance || user[0].balance
  console.log(username, password, phone, address, coupon, balance)

  await UserModel.updateOne(
    { _id: userId },
    { $set: { username, password, phone, address, coupon, balance } }
  )

  res.send({
    code: 0,
    msg: '修改成功'
    // data
  })
})

module.exports = router
